﻿using System.IO;
using System.Linq;
using System.Windows;
using TV_Organizer.Properties;
using System.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections;

namespace TV_Organizer
{
    public partial class PreferencesControl : UserControl
    {
        public PreferencesControl()
        {
            InitializeComponent();
            Settings.Default.SettingChanging += OnSettingsChanging;
            
            this.PropertiesControlDataContext = new PropertiesControlDataContext();
            this.DataContext = this.PropertiesControlDataContext;

            // Init the TVShowFolderPath description
            this.TVShowFolderPathDescription.Text = "Folders names can be composed of the following elements:";
            foreach(var part in FileScanner.FolderNameParser.Parts)
                this.TVShowFolderPathDescription.Text += string.Format("{0}    {1} : {2}", Environment.NewLine, part.ReplacedString, part.Description);
            this.TVShowFolderPathDescription.Text += string.Format("{0}    \\ : to make a subfolder", Environment.NewLine);
            
            this.TVShowFolderPathDescription.Text += string.Format("{0}Examples:", Environment.NewLine);
            this.TVShowFolderPathDescription.Text += string.Format("{0}    %show - Season %season2 : Supernatural - Season 05", Environment.NewLine);
            this.TVShowFolderPathDescription.Text += string.Format("{0}    %show - Season %season2\\%show S%season2E%episode2 : Supernatural S05E03", Environment.NewLine);
        }

        private PropertiesControlDataContext PropertiesControlDataContext { get; set; }

        private void AddFolderToScanButton_Click(object sender, RoutedEventArgs e)
        {
            var l_dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (l_dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (Directory.Exists(l_dialog.SelectedPath))
                    FileScanner.FileScanner.Instance.FoldersToScan.Add(l_dialog.SelectedPath);
            }
        }

        private void DeleteFolderToScanButton_Click(object sender, RoutedEventArgs e)
        {
            foreach(var folder in this.FoldersToScanListView.SelectedItems.OfType<string>().ToList())
                FileScanner.FileScanner.Instance.FoldersToScan.Remove(folder);
        }

        private void UpdateEPGuidesButton_Click(object sender, RoutedEventArgs e)
        {
            EPGuides.EPGuidesConnector.Instance.UpdateTVShows();
        }

        private void TVShowsFolderButton_Click(object sender, RoutedEventArgs e)
        {
            var l_dialog = new System.Windows.Forms.FolderBrowserDialog
            {
                SelectedPath = Settings.Default.TVShowsFolder,
            };
            if (l_dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (Directory.Exists(l_dialog.SelectedPath))
                    Settings.Default.TVShowsFolder = l_dialog.SelectedPath;
            }
        }

        private void ResetSettings_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default.Reset();
            MessageBox.Show("Settings resetted !");
            Settings.Default.Save();
        }

        private void OnSettingsChanging(object sender, System.Configuration.SettingChangingEventArgs e)
        {
            Settings.Default.Save();
        }

        private void EditTeamsButton_Click(object sender, RoutedEventArgs e)
        {
            var window = new StringListEditionWindow((IList)Settings.Default.Teams)
            {
                Title = "Edit teams list",
            };
            window.Closed += (o, e2) => Settings.Default.Save();
            window.ShowDialog();
        }

        private void ResetTVShowFolderPathButton_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default.TVShowFolderPath = (string)Settings.Default.Properties["TVShowFolderPath"].DefaultValue;
        }
    }
}
